package services;


import java.util.*;

import javax.jws.WebService;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.ws.BindingProvider;

import org.jboss.ws.core.StubExt;
import org.jboss.ws.annotation.EndpointConfig;

import data.DbTraining;
import edu.ipfw.fitness.EquipmentCategoryType;
import edu.ipfw.fitness.EquipmentReportDetailsType;
import edu.ipfw.fitness.EquipmentWorkoutListType;
import edu.ipfw.fitness.EquipmentWorkoutType;
import edu.ipfw.fitness.PatronWorkoutListType;
import edu.ipfw.services.equipment.Equipment;
import edu.ipfw.services.equipment.EquipmentNotFoundFault;
import edu.ipfw.services.equipment.EquipmentService;
import edu.ipfw.services.training.PatronNotFoundFault;
import edu.ipfw.services.training.TrainingService;

@WebService(serviceName = "Training", endpointInterface = "edu.ipfw.services.training.TrainingService", targetNamespace = "http://ipfw.edu/services/training/")
public class TrainingServiceImpl implements TrainingService {
	DbTraining db = new DbTraining();
	public boolean beginEquipmentWorkout(String patronId,
			XMLGregorianCalendar workoutDateTime, String equipmentId) {
		int patron;
		System.out.println("PatronID: " + patronId);
		System.out.println("DateTime: " + workoutDateTime);
		System.out.println("EquipmentID: " + equipmentId);
		try {
			patron = db.getPatron(patronId);
			int workout = db.getPatronWorkout(patron, workoutDateTime);
			int equipment = db.getEquipment(equipmentId);
//			System.out.println("PatronDBID: " + patron);
//			System.out.println("EquipmentDBID: " + equipment);
//			System.out.println("Workout: " + workout);
			return db.beginEquipmentWorkout(workout,equipment);
		} catch (PatronNotFoundFault e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
//			System.out.println("PatronID: " + patronId);
//			System.out.println("DateTime: " + workoutDateTime);
//			System.out.println("EquipmentID: " + equipmentId);
		}
		
		return false;
	}

	public boolean updateEquipmentWorkout(String patronId,
			XMLGregorianCalendar workoutDateTime,
			EquipmentWorkoutType equipmentWorkout) {
		try {
		int patron = db.getPatron(patronId);
		int equipment = db.getEquipment(equipmentWorkout.getEquipmentId());
		int pworkout = db.getPatronWorkout(patron, workoutDateTime);
		int workout = db.getWorkout(equipment, pworkout);
		return db.updateWorkout(workout,equipmentWorkout);
		} catch (PatronNotFoundFault e) {
			e.printStackTrace();
		}
		return false;
	}

	public boolean endEquipmentWorkout(String patronId,
			XMLGregorianCalendar workoutDteTime, String equipmentId) {
		try {
			int patron = db.getPatron(patronId);
			int pworkout = db.getPatronWorkout(patron, workoutDteTime);
			int equipment = db.getEquipment(equipmentId);
			int workout = db.getWorkout(equipment, pworkout);
			EquipmentWorkoutType ewt = db.getWorkoutDetails(workout,equipmentId);
			Equipment svc = new Equipment();
			EquipmentService port = svc.getEquipmentSOAP();
			GregorianCalendar c = new GregorianCalendar();
			c.setTime(new Date());
			float miles = 0;
			List<EquipmentReportDetailsType> workouts = ewt.getEquipmentWorkoutDetails();
			for (EquipmentReportDetailsType e : workouts) {
				miles += e.getDistance().floatValue();
			}
			return port.recordUsage(equipmentId, workoutDteTime, DatatypeFactory.newInstance().newXMLGregorianCalendar(c),miles);
			
		}catch (PatronNotFoundFault e) {
			
		} catch (EquipmentNotFoundFault e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DatatypeConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

/*	Creates a new patronWorkout and adds it to the Patron�s workout history. Returns timestamp with dateTime value of corresponding input parameter.
	Generates PatronNotFound fault if Patron does not exist.
*/
	public XMLGregorianCalendar createPatronWorkout(String patronId,
			XMLGregorianCalendar workoutDateTime) throws PatronNotFoundFault {
		System.out.println("Invoked createPatronWorkout");
		int patron = db.getPatron(patronId);
		if (db.createPatronWorkout(patron,workoutDateTime)) 
		{
			return workoutDateTime;
		}
		else
		{
			return null;
		}
			
	}

	public PatronWorkoutListType getWorkoutHistory(String patronId,
			XMLGregorianCalendar startDateTime, XMLGregorianCalendar endDateTime) throws PatronNotFoundFault {
		int patron = db.getPatron(patronId);
		return db.getPatronWorkoutHistory(patron,startDateTime,endDateTime, patronId);
	
	}

	public EquipmentWorkoutListType getEquipmentWorkoutHistory(String patronId,
			EquipmentCategoryType equipmentCategory,
			XMLGregorianCalendar startDateTime, XMLGregorianCalendar endDateTime)throws PatronNotFoundFault {
		int patron = db.getPatron(patronId);
		return db.getEquipmentWorkoutHistory(patron,equipmentCategory,startDateTime,endDateTime);
	}
}